<template>
  <!-- 顾客管理 -->
  <div>
    <!-- 头部部分 -->
    <div class="header">
      <!-- 添加按钮 -->
      <el-button @click="addCustome" type="primary">添加按钮</el-button>
      <!-- 产品名称输入框 -->
      <el-input
        style="width: 240px"
        placeholder="请输入名称"
        v-model="customeName"
        clearable
      >
      </el-input>
      <!-- 产品状态下拉框 -->
      <el-select
        style="width: 240px"
        v-model="customeStatus"
        clearable
        placeholder="请选择状态"
      >
        <el-option label="正常" value="正常"> </el-option>
        <el-option label="禁用" value="禁用"> </el-option>
      </el-select>

      <!-- 查询按钮 -->
      <el-button @click="selectBtn" type="primary">查询按钮</el-button>
    </div>

    <!-- 表格内容展示区域 -->
    <div class="table_content">
      <!-- :data 整个表格的数据源 -->
      <el-table :data="customData" style="width: 100%">
        <!-- 编号 -->
        <el-table-column type="index" label="编号" align="center">
        </el-table-column>
        <!-- 用户名 -->
        <el-table-column prop="username" label="用户名" align="center">
        </el-table-column>
        <!-- 姓名 -->
        <el-table-column prop="realname" label="姓名" align="center">
        </el-table-column>

        <!-- 手机号 -->
        <el-table-column prop="telephone" label="手机号" align="center">
        </el-table-column>
        <!-- 性别 -->
        <el-table-column prop="gender" label="性别" align="center">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.gender == 'male'" >{{male}}</el-tag>
            <el-tag v-else type="warning">{{female}}</el-tag>
          </template>
        </el-table-column>
        <!-- 状态 -->
        <el-table-column prop="status" label="状态" align="center">
          <template slot-scope="scope">
            <el-tag
              :type="scope.row.status === '正常' ? 'success' : 'danger'"
              disable-transitions
              >{{ scope.row.status }}</el-tag
            >
          </template>
        </el-table-column>

        <!-- 操作 -->
        <el-table-column label="操作" width="150" align="center">
          <template slot-scope="scope">
            <el-button @click="editCustome(scope.row)" type="text" size="small"
              >编辑</el-button
            >
            <el-button
              @click="clickDelete(scope.row.id)"
              style="color: red"
              type="text"
              size="small"
              >删除</el-button
            >
            <el-button
              @click="clickDetail(scope.row.id)"
              style="color: #3a71a8"
              type="text"
              size="small"
              >详情</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>

    <!-- 底部分页区域 -->
    <div class="paginetion">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[2, 4, 6, 8]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </div>

    <!-- 模态框 -->
    <el-dialog
      :title="dialogTitle"
      :visible.sync="dialogVisible"
      @close="dialogClose"
    >
      <!-- 表单区域 -->
      <!-- :model 表单对象 最终我们要提交的那个表单对象 -->
      <!-- :rules 表单验证规则 -->
      <!-- ref 类比与 id 最终通过ref找到这张表单 -->
      <el-form
        :model="customeForm"
        :rules="rules"
        ref="custome_form"
        label-width="80px"
      >
        <!-- 用户名 -->
        <el-form-item label="用户名" prop="username">
          <el-input v-model="customeForm.username"></el-input>
        </el-form-item>
        <!-- 姓名 -->
        <el-form-item label="姓名" prop="realname">
          <el-input v-model="customeForm.realname"></el-input>
        </el-form-item>
        <!--手机号  -->
        <el-form-item label="手机号" prop="telephone">
          <el-input v-model.number="customeForm.telephone"></el-input>
        </el-form-item>
        <!-- 密码 -->
        <el-form-item label="密码" prop="password">
          <el-input type="password" v-model="customeForm.password"></el-input>
        </el-form-item>
        <!-- 性别 -->
        <el-form-item label="性别" prop="gender">
          <el-radio v-model="customeForm.gender" label="male">男</el-radio>
          <el-radio v-model="customeForm.gender" label="female">女</el-radio>
        </el-form-item>
        <!-- 状态 -->
        <el-form-item v-if="flag == 1" label="状态" prop="status">
          <el-radio v-model="customeForm.status" label="正常">正常</el-radio>
          <el-radio v-model="customeForm.status" label="禁用">禁用</el-radio>
        </el-form-item>
      </el-form>
      <!-- 底部操作区域 -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogClose">取 消</el-button>
        <!-- 提交表单后调用重置表单的方法 -->
        <el-button v-if="flag == 1" type="primary" @click="editForm"
          >修 改</el-button
        >
        <el-button v-else type="primary" @click="submitForm">添 加</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
// 引入axios
import { get, post } from "@/utils/request";
export default {
  data() {
    return {
      male:'男 ♂',
      female: '女 ♀',
      // 顾客数组
      customData: [],
      // 当前的页码
      currentPage: 1,
      // 每页展示的数据条数
      pageSize: 8,
      // 页面展示数据的总条数
      total: 10,
      // 定义一个查询参数
      queryCustome: {},
      customeName: "",
      customeStatus: "",
      dialogVisible: false,
      dialogTitle: "",
      customeForm: {
        // 用户名
        username: "",

        // 姓名
        realname: "",
        // 手机号
        telephone: "",
        // 密码
        password: "",
        // 性别
        gender: "",
        // 状态
        status: "",
      },
      cusid: { roleId: 6 },
      // 表单验证规则
      rules: {
        // 用户名
        id: [{ required: true, message: "请输入用户名", trigger: "blur" }],
        // 姓名
        username: [{ required: true, message: "请输入姓名", trigger: "blur" }],
        // 手机号
        telephone: [
          { required: true, message: "请输入手机号", trigger: "blur" },
          // { type: "number", message: "手机号必须是数字" },
        ],
        // 密码
        password: [{ required: true, message: "请输入密码", trigger: "blur" }],
      },

      flag: 1,
    };
  },
  computed: {},
  methods: {
    async getAllCustomeData() {
      // 获取参数
      let data = {
        page: this.currentPage,
        pageSize: this.pageSize,
        roleId: 6,
      };
      let res = await get("/baseUser/pageQuery", {
        ...data,
        ...this.queryCustome,
      });

      // console.log(res.data.list);

      this.customData = res.data.list;

      this.total = res.data.total;
    },
    selectBtn() {
      let data = {
        username: this.customeName,
        status: this.customeStatus,
      };
      for (const key in data) {
        if (!data[key]) {
          delete data[key];
        }
      }
      this.queryCustome = data;

      this.currentPage = 1;

      this.getAllCustomeData();
    },
    handleSizeChange(val) {
      // 将每页要展示的数据的条数 进行一个更新
      this.pageSize = val;
      // console.log(`每页 ${val} 条`);
      // 将当前页的页数 重置到第一页
      this.currentPage = 1;
      // 重新调用获取所有产品的方法
      this.getAllCustomeData();
    },
    handleCurrentChange(val) {
      // 将当前页 与 分页插件动态点击控制的页数相绑定
      this.currentPage = val;
      // console.log(`当前页: ${val}`);
      // 重新调用获取产品数据的方法
      this.getAllCustomeData();
    },
    addCustome() {
      this.flag = 0;
      // 清空表单对象
      this.customeForm = {};
      // 设置模态框标题
      this.dialogTitle = "添加顾客信息";
      // 模态框显示
      this.dialogVisible = true;
    },
    editCustome(row) {
      this.flag = 1;
      // 深拷贝
      this.customeForm = { ...row };
      // console.log(this.customeForm);
      // 设置模态框标题
      this.dialogTitle = "修改顾客信息";

      // 模态框显示
      this.dialogVisible = true;
    },
    // 表单提交 更新或编辑产品信息
    submitForm() {
      this.$refs["custome _form"].validate(async (valid) => {
        if (valid) {
          // 发送表单验证的请求 提交表单对象
          let res = await post("/baseUser/addUserWidthRole", {
            ...this.customeForm,
            ...this.cusid,
          });
          console.log(res);
          // 给用户一个成功的提示
          this.$message({
            message: "提交成功",
            type: "success",
          });
          // 刷新页面
          this.getAllCustomeData();
          // 调用关闭模态框的方法
          this.dialogClose();
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    editForm() {
      this.$refs["custome_form"].validate(async (valid) => {
        if (valid) {
          // 发送表单验证的请求 提交表单对象
          let res = await post("/baseUser/saveOrUpdate", this.customeForm);
          console.log(res);
          // 给用户一个成功的提示
          this.$message({
            message: "提交成功",
            type: "success",
          });
          // 刷新页面
          this.getAllCustomeData();
          // 调用关闭模态框的方法
          this.dialogClose();
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    // 通过id删除顾客数据
    async clickDelete(id) {
      let res = await get("/baseUser/deleteById", { id });

      if (res.status == 200) {
        this.$message({
          message: res.message,
          type: "error",
        });
      } else {
        this.$message(error.message);
      }
      this.getAllCustomeData();
    },
    clickDetail(id) {
      // 通过$router.push()方法进行跳转
      this.$router.push({
        path: "/admin/custome/Details",
        // 获取到我的ID
        // 通过query对象进行路由传参
        query: {
          detailId: id,
        },
      });
    },
    // 关闭模态框  调用表单重置方法
    dialogClose() {
      // 重置表单
      this.$refs["custome_form"].resetFields();
      // 关闭模态框
      setTimeout(() => {
        this.dialogVisible = false;
      }, 200);
    },
  },
  created() {
    this.getAllCustomeData();
  },
  mounted() {},
};
</script>
<style scoped>
.header {
  line-height: 36px;
  height: 80px;
}
/* 分页区域样式 */
.paginetion {
  position: fixed;
  bottom: 28px;
  right: 20px;
}
</style>